package com.matt.controller;


import com.dotflat.util.excelUtil.ExcelDataVo;
import com.dotflat.util.excelUtil.PackExcelSheetsDataUtil;
import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.model.StatusResponse;
import com.matt.domain.ChildActivity;
import com.matt.foundation.ext.aliyun.OSSProcessService;
import com.matt.foundation.model.MemVerificationCache;
import com.matt.foundation.query.ChildrenActiveQuery;
import com.matt.foundation.query.ChildrenQuery;
import com.matt.foundation.utils.DateUtil;
import com.matt.foundation.vo.ChildActivityQuery;
import com.matt.foundation.vo.ChildActivityVo;
import com.matt.foundation.vo.children.WebChildrenVo;
import com.matt.service.ChildActivityService;
import io.swagger.annotations.*;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.matt.foundation.constant.RequestConstant.TICKET;


/**
 * (ChildActivity)表控制层
 *
 * @author makejava
 * @since 2025-02-21 15:04:17
 */
@RestController
@RequestMapping("childActivity")
public class ChildActivityController {


    @Autowired
    private ChildActivityService childActivityService;

    @Autowired
    private MemVerificationCache cache;

    @Autowired
    private OSSProcessService ossProcessService;

    @Value("${aliyun.oss.fileServerPrefix}")
    private String IMG_SERVICE_PREFIX;

    /**
     * 后台新增
     * @param ticket
     * @param title
     * @param classIds
     * @param type
     * @param address
     * @param startTime
     * @param endTime
     * @param deadlineTime
     * @param content
     * @param remark
     * @param id
     * @return
     */
    @ApiOperation(value = "新增或者编辑活动", httpMethod = "POST", hidden = false)
    @PostMapping("/addOrUpdate")
    public StatusResponse addOrUpdate(@RequestHeader(TICKET) String ticket,
                                 String title,
                                 String classIds,
                                 String type,
                                 String address,
                                 String startTime,
                                 String endTime,
                                 String deadlineTime,
                                 String content,
                                 String remark,
                                      String id
    ) {
        StaffDTO baseInfo = cache.getStaff(ticket);
        Integer source = 1;
        return StatusResponse.success(
                childActivityService.addOrUpdate(title,classIds,type, address, startTime, endTime, deadlineTime, content, remark,baseInfo,id,source)
        );
    }

    @ApiOperation(value = "活动列表",httpMethod = "POST")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public StatusResponse page(@RequestHeader(TICKET) String ticket, ChildrenActiveQuery query) {
        String areaId = cache.getAreaId(ticket);
        query.setAreaId(areaId);
        Page<ChildActivityVo> page = childActivityService.page(query);
        return StatusResponse.success(page);
    }


    @ApiOperation(value = "活动详情",httpMethod = "POST")
    @RequestMapping("/detail")
    @ResponseBody
    public StatusResponse detail(@RequestHeader(TICKET) String ticket, String activeId) {
        String areaId = cache.getAreaId(ticket);
        Map response = childActivityService.getDetailByActiveId(activeId,areaId);
        return StatusResponse.success(response);
    }

    @ApiOperation(value = "活动删除",httpMethod = "POST")
    @RequestMapping(value = "/delete")
    public StatusResponse delete(@RequestHeader(TICKET) String ticket,String id) {
        String areaId = cache.getAreaId(ticket);
        return StatusResponse.success(childActivityService.delete(id,areaId));
    }

    /**
     * 小程序新增
     * @param ticket
     * @param title
     * @param classIds
     * @param type
     * @param address
     * @param startTime
     * @param endTime
     * @param deadlineTime
     * @param content
     * @param remark
     * @param id
     * @return
     */
    @ApiOperation(value = "小程序新增或者编辑活动", httpMethod = "POST", hidden = false)
    @PostMapping("/smil/addOrUpdate")
    public StatusResponse addOrUpdate1(@RequestHeader(TICKET) String ticket,
                                      String title,
                                      String classIds,
                                      String type,
                                      String address,
                                      String startTime,
                                      String endTime,
                                      String deadlineTime,
                                      String content,
                                      String remark,
                                      String id
    ) {
        StaffDTO baseInfo = cache.getStaff(ticket);
        Integer source = 2;
        return StatusResponse.success(
                childActivityService.addOrUpdate(title,classIds,type, address, startTime, endTime, deadlineTime, content, remark,baseInfo,id,source)
        );
    }

    @ApiOperation(value = "导出")
    @GetMapping("/export")
    public StatusResponse export(@RequestHeader(TICKET) String ticket, String id,HttpServletResponse response) {
        StaffDTO baseInfo = cache.getStaff(ticket);
        List<ExcelDataVo> export = childActivityService.export(id, baseInfo.getAreaId());

        String url = "";
        // 导出数据
        try {
            url = new PackExcelSheetsDataUtil().packExcelSheetsData(response, "报名信息", export);
        } catch (Exception e) {
            throw new Exception("导出异常!");
        } finally {
            File outputFile = new File(url);
            String uploadPath = "teach/file/children_export_excel/" + DateTime.now().toString("yyyy-MM") + "/"  +"报名信息报表.xlsx";
            String relativePath = ossProcessService.aliyunOssUpload(uploadPath, outputFile);
            String uploadedResult = IMG_SERVICE_PREFIX + relativePath;
            return StatusResponse.success(uploadedResult);
        }

    }


    /**
     * 小程序列表
     */
    @ApiOperation(value = "活动列表",httpMethod = "POST")
    @RequestMapping(value = "/activeList", method = RequestMethod.POST)
    public StatusResponse activeList(@RequestHeader(TICKET) String ticket,@RequestBody ChildActivityQuery query) {
        String areaId = cache.getAreaId(ticket);
        List<ChildActivityVo> list = childActivityService.activeList(areaId,query.getRoleName(),query.getClassId());
        return StatusResponse.success(list);
    }
}

